package com.xhwl.centiotdata.handler.operation.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.operation.OperationIntegrationFactory;
import com.xhwl.centiotdata.handler.operation.OperationIntegrationHandler;
import com.xhwl.centiotdata.suppliersystem.bailian.CompanyBaiLianHandler;
import com.xhwl.centiotdata.suppliersystem.bailian.SaleBaiLianHandler;
import com.xhwl.common.enums.centsecurity.WeekEnum;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.utils.CalendarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/20/ 10:06
 */
@Service
public class OperationIntegrationBaiLianHandler extends OperationIntegrationHandler {

    @Autowired
    private CompanyBaiLianHandler companyBaiLianHandler;

    @Autowired
    private SaleBaiLianHandler saleBaiLianHandler;

    @Override
    public void afterPropertiesSet() throws Exception {
        OperationIntegrationFactory.register(9, this);
    }

    /**
     * 每天一次
     *
     * @param deviceProjSupplierCategory
     */
    @Override
    public void pullCompany(DeviceProjSupplierCategory deviceProjSupplierCategory) {
        JSONObject properties = deviceProjSupplierCategory.getProperties();
        String ipAndPort = properties.getString("ipAndPort");
        companyBaiLianHandler.syncCompany(deviceProjSupplierCategory.getProjectId(), ipAndPort);
    }


    //次日同步昨日数据时间点，  因为发现00:20同步的数据和 10:00同步的数据不一致，所以多加两个时间点
    private static final String[] SYNC_YESTERDAY_CLOCK = new String[]{"00:20", "09:00", "10:00", "12:00", "18:00"};


    //周一9点，同步上周数据
    private static final String[] SYNC_LAST_WEEK_CLOCK = new String[]{"9:00"};

    /**
     * 十分钟一次
     *
     * @param deviceProjSupplierCategory
     */
    @Override
    public void pullSales(DeviceProjSupplierCategory deviceProjSupplierCategory) {
        JSONObject properties = deviceProjSupplierCategory.getProperties();
        Integer projectId = deviceProjSupplierCategory.getProjectId();
        String ipAndPort = properties.getString("ipAndPort");
        if (isLastWeekSyncTime()) {
            //拉取上一周数据
            saleBaiLianHandler.pullSalesDays(projectId, ipAndPort, 7);
        } else if (isYesterdaySyncTime()) {
            //如果是凌晨，同步昨天的数据
            saleBaiLianHandler.pullSalesYesterday(projectId, ipAndPort);
        }
        //同步当日数据
        saleBaiLianHandler.pullSalesToday(projectId, ipAndPort);
    }

    //每周周一早上9点更新上一周的数据
    private boolean isLastWeekSyncTime() {
        WeekEnum week = CalendarUtil.getWeek(new Date());
        return week == WeekEnum.MONDAY && isInClock(SYNC_LAST_WEEK_CLOCK);
    }

    private boolean isYesterdaySyncTime() {
        return isInClock(SYNC_YESTERDAY_CLOCK);
    }

    private boolean isInClock(String[] clocks) {
        SimpleDateFormat sbf = new SimpleDateFormat("HH:mm");
        String currentHour = sbf.format(new Date());
        for (int i = 0; i < clocks.length; i++) {
            String s = clocks[i];
            if (s.equals(currentHour))
                return true;
        }
        return false;
    }

}
